package com.KuoHaoPiPei;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一元算术表达式");
        String str = sc.next();
        char[] chars = str.toCharArray();
        boolean matching = matching(chars);
        if (matching) {
            System.out.println("表达式中括号匹配");
        }else {
            System.out.println("表达式中括号不匹配");
        }

    }
    public static boolean matching(char[] chars) {
        int i = 0;
        int state = 1;
        SequenceStack<Character> strs = new SequenceStack<>();
        while (i < chars.length && state == 1) {
            switch (chars[i]) {
                case '[':
                case '(':
                    strs.push(chars[i]);
                    i++;
                    break;
                case ']':
                    if (!strs.isEmpty() && strs.getHead() == '[') {
                        System.out.println("删除栈顶元素：" + strs.pop());
                        i++;
                    } else {
                        state = 0;
                    }
                    break;
                case ')':
                    if (!strs.isEmpty() && strs.getHead() == '(') {
                        System.out.println("删除栈顶元素：" + strs.pop());
                        i++;
                    } else {
                        state = 0;
                    }
                    break;
                default:
                    i++;
                    break;

            }
        }
        return state == 1 && strs.isEmpty();
    }

}

